﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Validation.sp2010.Helper
{
  public  class HelperUtilities
    {
      public static string GetResources(string resourceName, uint language)
      {
          return SPUtility.GetLocalizedString("$Resources:" + resourceName, "ResourceValidationWF", language);
      }
      public static SPUser ResolveUserFromLogin(SPWeb web, string login)
      {
          try
          {
              SPUser user = web.EnsureUser(login);
              return user;
          }
          catch (SPException ex)
          {
              return null;
          }
      }

      public static void AttachContentTypeToList(SPWeb web, SPList list, SPContentTypeId contentTypeId)
      {
          if (!list.ContentTypesEnabled)
          {
              list.ContentTypesEnabled = true;
              list.Update();
          }
          if (list.ContentTypes.Cast<SPContentType>().Count(c => c.Parent.Id == contentTypeId) == 0)
          {
              list.ContentTypes.Add(web.AvailableContentTypes[contentTypeId]);
              list.Update();
          }

      }

      public static string GetValueLookup(string lookUpField)
      {
          string[] vars = lookUpField.Split(";#".ToArray());
          return vars.LastOrDefault();
      }
    }
}
